package com.tao.ai.controller;

import com.tao.ai.entity.User;
import com.tao.ai.service.LoginService;
import com.tao.ai.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {

    @Autowired
    private LoginService loginservice;

    private String userName="";

    @RequestMapping("/juge")
    public ModelAndView login(String username, String password){
        User user=loginservice.findByName(username);
        if(user.getPassword().equals(password)){
            switch(user.getType()){
                case 1 :
                    return new ModelAndView("redirect:Teacher.html");
                default:
                    return new ModelAndView("redirect:Student.html");
            }
        }
        else{
            return new ModelAndView("redirect:Login.html");
        }
    }

    @RequestMapping(value = "/setUserName",method = RequestMethod.POST)
    @ResponseBody
    public void setUserName(String ruserName){
        userName=ruserName;
        return;
    }

    @RequestMapping(value = "/getUserId",method = RequestMethod.POST)
    @ResponseBody
    public LoginVo getUserId(){
        Integer type=loginservice.findByName(userName).getType();
        Integer userId=loginservice.findByName(userName).getUserId();
        LoginVo loginVo=new LoginVo();
        loginVo.setUsername(userName);
        if(type==1){
            loginVo.setId(loginservice.findByUserIdT(userId));
        }else{
            loginVo.setId(loginservice.findByUserIdS(userId));
        }
        return loginVo;
    }
}
